<template>
	<view class="">
		<!-- 导航栏 -->
		<view class="nav pa x-f" :style="{top: `${top + 3}px`}">
			<image @tap.stop="backRouter" :src="back_ico" style="width: 48rpx; height: 48rpx;"></image>
			<view class="nav-title pa tc">{{goods_title.label}}</view>
		</view>
		<image class="bg" :src="choiceness"></image>
		<view class="dropdown pa">
			<dropdown-menu :top="top" @selectOption="selectOption"></dropdown-menu>
		</view>
		<view class="main pa">
			<!-- 商品信息 -->
			<view class="goods"  @touchstart="touchStart($event)" @touchmove="touchMove($event)" @touchend="touchEnd($event)">
				<refresh :freshFlag="freshFlag" :loadMsg="loadMsg"></refresh>
				<goods-list :goods_data="goods" @getId="getId" @clear="clear" ref="goodsList"></goods-list>
				<view class="" style="padding-bottom: 50rpx;" v-if="goods.length">
					<touch-bottom :isNone="isNone" :isLoad="isLoad" :isName="isName"></touch-bottom>
				</view>
			</view>
		</view>
		<empty :null_data="null_data" v-if="goods.length == 0"></empty>
		<!-- 返回顶部 -->
		<backtop :show_top="show_top"></backtop>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	export default {
		data() {
			return {
				show_top: false,                                // 回到顶部按钮显示
				null_data: {                                    // 空状态
					null_img: this.imgUrl + 'nocollect.png',
					text: '暂无商品~'
				},
				isName: false,                                  // 触底加载
				isLoad: false,									// 加载中
				isNone: false,									// 触底加载隐藏
				img: this.imgUrl + '3.png', 					// 分类导航的图标
				choiceness: this.imgUrl + 'choicenss-bg.png',   // 背景图片
				back_ico: this.imgUrl + 'back.png',				// 返回按钮
				obj: {  										// 请求参数
					page: 1,
					pagesize: 10,
					category_id: '',
					orderby_type: 'number',
					orderby: 'desc',
					lat: '24.881627',
					lng: '118.628881',
					member_id: 1
				},
				goods: [], 										// 商品
				giftPage: [],									// 商品分类标题
				page: 1,										// 页数
				goodsMore: [], 									// 加载更多商品
				num: '' ,										// 下拉下标
				totalPages: 0, 									// 总页数
				startY: 0, 										// 保存 y轴点的位置
				touching: false, 								// 代表当前是否处于 下拉刷新行为的开关，也就是当属于滚动行为时，就要退出该事件机制
				loadShowHeight: 0,								// 下拉高度
				scrollTop: 0,									// 滚动高度
				loadMinHeight: 0,								// 最大高度
				refreshType: false,								// 刷新状态
				loadMsg: '',									// 加载文字
				freshFlag: false,								// 状态
				hight: 0,										// 高度
			}
		},
		computed: {
			...mapState({
				category_a: state => state.sellers.category_a,  // 分类
				systemInfo: state => state.init.systemInfo, 	// 设备信息
				goods_title: state => state.goods.title, 		// 商品分类
				top: state => state.init.menuButtonInfo.top ,	// 胶囊距离顶部高度
				Location: state => state.user.Location, 		// 经纬度,
			})
		},
		watch: {
			// 监听下拉下标
			num(newVal, oldVal) {
				if (newVal != '00') {
					if (newVal != oldVal) {
						this.clear();
						this.getApi();
					}
				}
			},
			// 监听滚动高度
			hight(newVal, oldVal) {
				if (newVal > 100) {
					this.show_top = true;
				} else {
					this.show_top = false;
				}
			}
		},
		methods: {
			// 当页面滚动时触发;
			onPageScroll(res) {
			    // 获取当前页面滚动高度；
				this.hight = res.scrollTop;
			},
			touchStart(e) {
				this.startY = e.touches[0].pageY;
			},
			touchMove(e) {
				if (this.scrollTop == 0) {
					let moveDistance = e.touches[0].pageY - this.startY;
					if (moveDistance > 0 && moveDistance <= (this.loadMaxHeight || 30)) {
						this.loadShowHeight = moveDistance;
					};
				};
			},
			touchEnd(e) {
				if (this.loadShowHeight >= (this.loadMinHeight || 20)) {
					this.refresh();
				} else {
					this.loadShowHeight = 0;
				}
			},
			refresh() {
				this.freshFlag = true;
				this.loadMsg = '刷新中...';
				setTimeout(() => {
					this.getMore();
					this.loadMsg = '刷新完成';
					this.loadShowHeight = 0;
					setTimeout(()=> {
						this.freshFlag = false;
					},500)
				}, 1500);
			},
			clear() {
				this.$refs.goodsList.clear();
			},
			// 下拉菜单
			selectOption(index, active) {
				this.isNone = true
				this.isLoad = false
				this.isName = false
				let a = index.toString();
				let b = active.toString();
				this.num = a + b;
				this.obj.page = 0;
				if (index == 0) {
					if (active == 1) {
						this.obj.orderby_type = 'number'
						this.obj.orderby = 'asc'
					}
					if (active == 2) {
						this.obj.orderby_type = 'distance'
						this.obj.orderby = 'asc'
						this.obj.pagesize = 14
					}
				}
				if (index == 1) {
					if (active == 0) {
						this.obj.orderby_type = 'number'
						this.obj.orderby = 'desc'
					}
					if (active == 1) {
						this.obj.orderby_type = 'number'
						this.obj.orderby = 'asc'
						this.obj.pagesize = 8
					}
				}
				if (index == 2) {
					if (active == 0) {
						this.obj.orderby_type = 'distance'
						this.obj.orderby = 'asc'
						this.obj.pagesize = 12
					}
					if (active == 1) {
						this.obj.orderby_type = 'distance'
						this.obj.orderby = 'desc'
					}
				}
			},
			// 获取商品id
			getId(e) {
				this.$store.dispatch('goods/get_detail', e)
			},
			// api 请求
			getApi() {
				this.$api('giftpage', this.obj).then(res => {
					this.totalPages = res.data.totalPages
					if (!res.data.data.length) {} else {
						this.goods = res.data.data
						this.goodsMore = res.data.data
					}
				})
			},
			// 请求商品
			getGoods() {
				this.getApi()
			},
			backRouter() {
				this.$Router.back(1)
			},
			getMore() {
				this.isNone = true
				this.isLoad = true
				this.isName = false
				if (!this.goods.length) {
					this.isNone = true
					this.isLoad = false
					this.isName = true
				} else {
					if(this.obj.page <= this.totalPages) {
						this.$api('giftpage', this.obj).then(res => {
							if (!res.data.data.length) {
								this.isNone = true
								this.isLoad = false
							} else {
								this.goodsMore = res.data.data
								this.goods = this.goodsMore
							}
							this.$store.dispatch('goods/get_giftPage', this.goods)
						})
					} else {
						this.isNone = true
						this.isLoad = false
						this.isName = true
					}
				}
			},
		},
		mounted() {
			this.obj.lat = Number(this.Location.latitude)
			this.obj.lng = Number(this.Location.longitude)
			this.obj.category_id = this.goods_title.id;
			this.getGoods()
		},
		onReachBottom() {
			setTimeout(() => {
				this.obj.page++
				this.isLoad = false
				this.getMore()
			}, 1000)
		},
	}
</script>

<style lang="scss" scoped>
	@import './index.scss';
</style>
